package com.example.mobile2032021606168xmy0618;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder>{
    private List<Users>list;
    public UsersRViewAdapter(List<Users> list){this.list=list; }
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemId,tvItemName,tvItemAge,tvItemScore;
        Button btnItemUpdate,btnItemDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemId=itemView.findViewById(R.id.tv_item_id);
            tvItemName=itemView.findViewById(R.id.tv_item_name);
            tvItemAge=itemView.findViewById(R.id.tv_item_age);
            tvItemScore=itemView.findViewById(R.id.tv_item_score);
            btnItemUpdate=itemView.findViewById(R.id.btn_item_update);
            btnItemDelete=itemView.findViewById(R.id.btn_item_delete);
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_main_user,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Users users=list.get(position);
        holder.tvItemId.setText(String.valueOf(users.get_id()));
        holder.tvItemName.setText(users.getName());
        holder.tvItemAge.setText(String.valueOf(users.getAge()));
        holder.tvItemScore.setText(String.valueOf(users.getScore()));
        holder.btnItemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyDataAdapter().delete(v.getContext(),list.get(position).get_id());
                list.clear();
                list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                notifyDataSetChanged();

            }
        });
        holder.btnItemUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view=LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_insert,null);
                final EditText edtTxtDialogName=view.findViewById(R.id.edtTxt_dialog_name);
                final EditText edtTxtDialogAge=view.findViewById(R.id.edtTxt_dialog_age);
                final EditText edtTxtDialogScore=view.findViewById(R.id.edtTxt_dialog_score);
                edtTxtDialogName.setText(list.get(position).getName());
                edtTxtDialogAge.setText(String.valueOf(list.get(position).getAge()));
                edtTxtDialogScore.setText(String.valueOf(list.get(position).getScore()));
                new AlertDialog.Builder(v.getContext())
                        .setTitle("更新数据")
                        .setView(view)
                        .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name=edtTxtDialogName.getText().toString().trim();
                                int age=Integer.valueOf(edtTxtDialogAge.getText().toString().trim());
                                float score=Float.valueOf(edtTxtDialogScore.getText().toString().trim());
                                new MyDataAdapter().update(v.getContext(),
                                        list.get(position).get_id(),
                                        new Users(name,age,score));
                                list.clear();
                                list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                                notifyDataSetChanged();

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();



    }
}
